<?php


namespace app\backstage\controller;


use app\backstage\classes\helper\XlsTools;
use app\backstage\logic\OpenLogic;
use app\backstage\logic\RechargeLogic;
use app\backstage\logic\WebsiteLogic;
use app\common\model\Banner;
use app\common\model\QuickOpen;
use app\common\model\RechargeActivity;
use app\common\model\Recommend;
use app\common\model\RecommendNovel;
use app\common\model\SignConfig;
use think\Controller;
use think\Db;
use think\facade\Request;

class Website extends Controller
{
    protected $middleware = [
        'VerifyJwt' => ['except' => []],
        'VerifyPower' => ['except' => []],
    ];

    protected $logic;

    public function __construct(WebsiteLogic $logic = null)
    {
        $this->logic = $logic;
        parent::__construct();
    }

    /*
     * 联系客服
     * */
    public function customer()
    {
        $Customer = \think\facade\App::model('Customer', 'logic');

        if( Request::isGet() ){

            $customer = $Customer->getInfo();

            $this->assign('customer',$customer);
            return $this->fetch();
        }

        $params = Request::post('','','strip_tags,trim');
        $result = $Customer->setInfo($params);

        return json($result);
    }

    /**
     * 内容配置
     * @return mixed
     */
    public function defaultBookshelf()
    {
        $agent_id = input('agent_id');
        $type = input('type', 1);
        $user = request()->_user;
        $role = request()->_role;

        $agents = getAgents();

        $this->assign('agent_id',$agent_id);
        $this->assign('agents', $agents);
        $this->assign('role', $role);
        $this->assign('type', $type);
        return $this->fetch();
    }


    /**
     * 签到配置
     * @return \think\response\Json
     */
    public function signConfig()
    {
        $quickId = input('quick_id');
        if (!$quickId) {
            $this->error('选择App');
        }
        $role = request()->_role;
        $user = request()->_user;
        $where[] = ['quick_id', '=', $quickId];
        if ($role->alias == 'agent') {
            $where[] = ['manage_id', '=', $user->id];
        } else {
            $where[] = ['manage_id', '=', 0];
        }
        $quick = \app\common\model\QuickApp::get($quickId);
        if (!$quick) {
            $this->error('App不存在');
        }
        $defaultTemplate = SignConfig::defaultTemplate($quick->info->sign_max);
        $mySignData = SignConfig::where($where)->value('sign_data');
        $mySignData = json_decode($mySignData, true);
        $data = [];
        if ($mySignData) {
            foreach ($defaultTemplate as $k => $v) {
                if (isset($mySignData[$k])) {
                    $data[$k] = $mySignData[$k];
                } else {
                    $data[$k] = $v;
                }
            }
        } else {
            $data = $defaultTemplate;
        }
        return sucJson($data);
    }

    /**
     * 获取类型书单
     * @return \think\response\Json
     * @Author: whh
     */
    public function bookshelfNovels()
    {
        $agent_id = input('agent_id');
        if (strlen($agent_id) < 1) {
            return sucJson([], 0);
        }
        $where = [];
        $id = input('id');
        $title = input('title');
        $scale = input('scale');
        $freq_id = input('freq_id');
        $class_id = input('class_id');
        $update_status = input('update_status');

        $page = input('page', 1);
        $limit = input('limit', 20);

        if ($id != '') {
            $where[] = ['id', '=', $id];
        }
        if ($title != '') {
            $where[] = ['title', 'like', "%{$title}%"];
        }
        if ($scale != '') {
            $where[] = ['scale', '=', $scale];
        }
        if ($freq_id != '') {
            $where[] = ['freq_id', '=', $freq_id];
        }
        if ($class_id != '') {
            $where[] = ['class_id', '=', $class_id];
        }
        if ($update_status != '') {
            $where[] = ['update_status', '=', $update_status];
        }
        return $this->logic->getBookshelfNovels($where, $page, $limit);
    }


    /**
     * 添加默认书架
     * @return \think\response\Json
     * @throws \Exception
     */
    public function addBookshelfNovelIds()
    {
        $agent_id = input('agent_id');
        $nIds = input('nIds');
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\WebsiteValidate.addBookshelfNovelIds');
        if (true !== $result) {
            $this->error($result);
        }
        return $this->logic->addBookshelfNovelIds($agent_id, $nIds);
    }

    /**
     * 删除书籍
     * @return \think\response\Json
     * @throws \Exception
     */
    public function delBookshelfNovel()
    {
        $quickId = input('quick_id');
        $novelIds = input('novel_ids');
        $type = input('type');
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\WebsiteValidate.delBookshelfNovel');
        if (true !== $result) {
            $this->error($result);
        }
        return $this->logic->delBookshelfNovel($quickId, $type, $novelIds);
    }

    /**
     * 设置签到书点
     * @return \think\response\Json
     * @Author: whh
     */
    public function setSignPoint()
    {
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\WebsiteValidate.setSignPoint');
        if (true !== $result) {
            $this->error($result);
        }
        $index = input('index');
        $point = input('point');
        $quickId = input('quick_id');
        return $this->logic->setSignPoint($quickId, $index, $point);
    }

    /**
     * 设置任务中心
     * @return \think\response\Json
     */
    public function editTask()
    {
        if (request()->isAjax()) {
            $params = request()->param();
            $result = $this->validate($params, 'app\backstage\validate\WebsiteValidate.editTask');
            if (true !== $result) {
                $this->error($result);
            }
            $key = input('key');
            $str = '';
            if ($key == 'calendar') {
                $str = 'calendar';
            } elseif ($key == 'read') {
                $str = 'taskRead';
            }
            if ($str) {
                $result = $this->validate($params, 'app\backstage\validate\WebsiteValidate.' . $str);
                if (true !== $result) {
                    $this->error($result);
                }
            }
            if ($key == 'calendar') {
                $today = date("Y-m-d ");
                $startDate = $today . input('startDate');
                $endDate = $today . input('endDate');
                if ($startDate > $endDate) {
                    $this->error('参数异常：开始时间大于结束时间');
                }
            }
            $this->logic->setTask();
            $this->success();
        } else {
            return $this->editTaskForm();
        }
    }


    /**
     * 修改任务表单
     * @return mixed
     */
    private function editTaskForm()
    {
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\WebsiteValidate.editTaskForm');
        if (true !== $result) {
            $this->error($result);
        }
        $key = input('key');
        $quick_id = input('quick_id');
        $taskData = $this->logic->getTaskData($quick_id);
        $this->assign('quick_id', $quick_id);
        $this->assign('key', $key);
        $this->assign('info', $taskData[$key]);
        return $this->fetch('website/task/' . $key);
    }

    /**
     * 推荐位设置
     * @return mixed
     */
    public function banner()
    {
        $status = input('status', 1);
        $agent_id = input('agent_id');
        $user = request()->_user;
        $role = request()->_role;
        $agents = getAgents();
        if (!in_array($status, [0, 1, 2])) {
            $this->error('参数异常');
        }
        $this->assign('agents', $agents);
        $this->assign('agent_id', $agent_id);
        $this->assign('role', $role);
        $this->assign('status', $status);
        return $this->fetch();
    }


    /**
     * @return \think\response\Json
     */
    public function bannerList()
    {
        $agent_id = input('agent_id');
        $seat = input('seat');
        $type = input('type');
        $kw = input('kw');
        $content_type = input('content_type');

        $page = input('page', 1);
        $limit = input('limit', 20);
        $where = [];
        if ($agent_id != '') {
            $where[] = ['agent_id', '=', $agent_id];
        }
        if ($seat != '') {
            $where[] = ['seat', '=', $seat];
        }
        if ($type != '') {
            $where[] = ['type', '=', $type];
        }
        if ($content_type != '') {
            $where[] = ['content_type', '=', $content_type];
        }
        if ($kw != '') {
            $where[] = ['novel_id|activity_id', '=', $kw];
        }
        return $this->logic->bannerList($where, $page, $limit);
    }


    /**
     * 设置banner
     * @Author: whh
     */
    public function setBanner()
    {
        if (request()->isAjax()) {
            $params = $this->request->param();
            $result = $this->validate($params, 'app\backstage\validate\WebsiteValidate.setBanner');
            if (true !== $result) {
                $this->error($result);
            }
            $activity_id = input('activity_id');
            $activity = RechargeActivity::get($activity_id);
            if (!$activity) {
                $this->error('请选择推广活动');
            }
            return $this->logic->setBanner($params);
        }
        $id = input('id');
        $info = Banner::get($id);
        $user = request()->_user;
        $agents = getAgents();

        $agent_list[] = ['name'=>'分销平台','value'=>'0','selected'=>false];
        foreach ($agents as $k=>$v){
            $agent_list[] = [
                'name'      => $v['account'],
                'value'     => $v['id'],
                'selected'  => $info && $info['agent_id'] == $v['id'] ? true : false
            ];
        }

        $this->assign('agent_list', json_encode($agent_list, JSON_UNESCAPED_UNICODE));
        $this->assign('info', $info);
        $allActivity = RechargeLogic::allActivity();
        $this->assign('allActivity', $allActivity);
        return $this->fetch();
    }

    /**
     * 推荐位
     * @return mixed
     */
    public function recommend()
    {
        $user = request()->_user;
        $agents = getAgents();
        $agent_id = input('agent_id');

        $this->assign('agent_id',$agent_id);
        $this->assign('agents', $agents);
        return $this->fetch();
    }

    /**
     * 推荐位列表
     * @return \think\response\Json
     * @Author: whh
     */
    public function recommendList()
    {
        $agent_id = input('agent_id');
        $freq = input('freq');
        $title = input('title');
        $type = input('type');
        $isRandom = input('is_random');
        $novel_id = input('novel_id');

        $page = input('page', 1);
        $limit = input('limit', 20);
        $where = [];
        if ($agent_id != '') {
            $where[] = ['agent_id', '=', $agent_id];
        }
        if ($freq != '') {
            $where[] = ['freq', '=', $freq];
        }
        if ($title != '') {
            $where[] = ['title', 'like', "%{$title}%"];
        }
        if ($type != '') {
            $where[] = ['type', '=', $type];
        }
        if ($isRandom != '') {
            $where[] = ['is_random', '=', $isRandom];
        }
        if ($novel_id != '') {
            $rids = RecommendNovel::where('novel_id', $novel_id)->column('rid');
            $where[] = ['id', 'in', $rids];
        }
        return $this->logic->recommendList($where, $page, $limit);
    }

    /**
     * 删除banner
     * @return \think\response\Json
     * @throws \Exception
     */
    public function delBanner()
    {
        $ids = input('ids');
        $params = $this->request->param();
        $result = $this->validate($params, 'app\backstage\validate\WebsiteValidate.delBanner');
        if (true !== $result) {
            $this->error($result);
        }
        return $this->logic->delBanner($ids);
    }

    /**
     * 设置推荐位
     * @return mixed|\think\response\Json
     * @Author: whh
     */
    public function setRecommend()
    {
        if (request()->isAjax()) {
            $params = $this->request->param();
            $recomId = isset($data['id']) && $data['id'] ? $data['id'] : 0;
            if (!$recomId){
                $result = $this->validate($params, 'app\backstage\validate\WebsiteValidate.addRecommend');
            }else{
                $result = $this->validate($params, 'app\backstage\validate\WebsiteValidate.editRecommend');
            }
            if (true !== $result) {
                $this->error($result);
            }
            return $this->logic->setRecommend($params);
        }

        $user = request()->_user;
        $agents = getAgents();
        $this->assign('agents', $agents);

        $id = input('id');
        if ($id) {
            $info = Recommend::get($id);
            if (!$info) {
                $this->error('推荐位不存在');
            }
            $this->assign('info', $info);
            return $this->fetch('website/edit_recommend');
        }
        return $this->fetch('website/add_recommend');
    }

    /**
     * 删除推荐位
     * @return \think\response\Json
     * @throws \Exception
     * @Author: whh
     */
    public function delRecommend()
    {
        $ids = input('ids');
        $params = $this->request->param();
        $result = $this->validate($params, 'app\backstage\validate\WebsiteValidate.delRecommend');
        if (true !== $result) {
            $this->error($result);
        }
        return $this->logic->delRecommend($ids);
    }

    /**
     * 推荐位书籍
     * @return mixed|\think\response\Json
     * @Author: whh
     */
    public function recommendNovel()
    {
        if (request()->isAjax()) {
            $id = input('id');
            $novelId = input('novel_id');
            $type = input('type');
            $page = input('page', 1);
            $limit = input('limit', 10);
            $where = [];
            if ($id != '') {
                $where[] = ['rid', '=', $id];
            }
            if ($novelId != '') {
                $where[] = ['novel_id', '=', $novelId];
            }
            if ($type != '') {
                $where[] = ['type', '=', $type];
            }
            return $this->logic->recommendNovel($where, $page, $limit);
        }
        $id = input('id');
        $recommend = Recommend::get($id);
        if (!$recommend) {
            $this->error('推荐位不存在！');
        }

        if($recommend['agent_id'] > 0)
        {
            $agents = Db::table('cms_manager_user')->where('id',$recommend['agent_id'])
                ->field('account,remarks')->findOrEmpty();
            $agent_name = $agents['account']."【{$agents['remarks']}】";
        }else{
            $agent_name = '分销平台';
        }

        $recommend['agent_name'] = $agent_name;

        $this->assign('recommend', $recommend);
        $this->assign('id', $id);
        return $this->fetch();
    }


    /**
     * 添加推荐位书籍
     * @return \think\response\Json
     * @throws \Exception
     */
    public function addRecommendNovel()
    {
        $rId = input('id');
        $nIds = input('nIds');
        $type = input('type');
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\WebsiteValidate.addRecommendNovel');
        if (true !== $result) {
            $this->error($result);
        }
        return $this->logic->addRecommendNovel($rId, $type,$nIds);
    }

    /**
     * 设置推荐位书籍排序
     * @return \think\response\Json
     * @Author: whh
     */
    public function setRecommendNovelSort()
    {
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\WebsiteValidate.setRecommendNovelSort');
        if (true !== $result) {
            $this->error($result);
        }
        $where[] = ['id', '=', $params['id']];
        RecommendNovel::where($where)->setField('sort', $params['sort']);
        return sucJson();
    }

    /**
     * 删除推荐位书籍
     * @return \think\response\Json
     * @throws \Exception
     * @Author: whh
     */
    public function delRecommendNovel()
    {
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\WebsiteValidate.delRecommendNovel');
        if (true !== $result) {
            $this->error($result);
        }
        $where[] = ['id', '=', $params['id']];
        RecommendNovel::where($where)->delete();
        return sucJson();
    }

    /**
     * 默认推荐位模板
     * @return mixed
     */
    public function plAddRecommend()
    {
        if (request()->isAjax()) {
            $params = $this->request->param();
            $data = [];
            foreach ($params['title'] as $k => $v){
                $data[$k]['title'] = $v;
                $data[$k]['agent_id'] = $params['agent_id'];
            }
            foreach ($params['freq'] as $k => $v){
                $data[$k]['freq'] = $v;
            }
            foreach ($params['default'] as $k => $v){
                if ($v != 1){
                    unset($data[$k]);
                }
            }
            if (!$data){
                $this->error('请添加推荐位');
            }
            foreach ($data as $k => $v) {
                $result = $this->validate($v, 'app\backstage\validate\WebsiteValidate.addRecommend');
                if (true !== $result) {
                    $this->error($result);
                }
            }
            $res = [];
            foreach ($data as $k => $v) {
                $resK= $this->logic->setRecommend($v);
                if ($resK->getData()['code'] == 1){
                    $res[$v['title']]  = ['code' => 1,'msg' => '成功'];
                }else{
                    $res[$v['title']]  = ['code' => 0,'msg' => $resK->getData()['msg']];
                }
            }
            return sucJson($res);
        } else {
            $template = Recommend::defaultTemplate();
            $this->assign('template', $template);
            $user = request()->_user;
            $agents = getAgents();
            $this->assign('agents', $agents);
            return $this->fetch();
        }
    }

    /**
     * 导出推荐位
     * @Author: whh
     */
    public function recommendExport(){
        $quick_id = input('quick_id');
        $freq = input('freq');
        $title = input('title');
        $type = input('type');
        $isRandom = input('is_random');
        $novel_id = input('novel_id');

        $where = [];
        if ($quick_id != '') {
            $where[] = ['quick_id', '=', $quick_id];
        }
        if ($freq != '') {
            $where[] = ['freq', '=', $freq];
        }
        if ($title != '') {
            $where[] = ['title', 'like', "%{$title}%"];
        }
        if ($type != '') {
            $where[] = ['type', '=', $type];
        }
        if ($isRandom != '') {
            $where[] = ['is_random', '=', $isRandom];
        }
        if ($novel_id != '') {
            $rids = RecommendNovel::where('novel_id', $novel_id)->column('rid');
            $where[] = ['id', 'in', $rids];
        }
        $field = "*";
        $orderBy = "id desc";
        $recomends = Recommend::getList($where, $field, $orderBy, 0, 0, ['quickApp']);
        $list = [];
        foreach ($recomends as $k => $v){

            if ($v['freq'] == 1){
               $freq_name = '男频';
            }elseif ($v['freq'] == 2){
                $freq_name = '女频';
            }elseif ($v['freq'] == 0){
                $freq_name = '无区分';
            }
            $location_key = $v['location_key'];
            $list[$v['quick_app']['id']]['quick_name'] = $v['quick_app']['name'];
            $list[$v['quick_app']['id']]['list'][$v['freq']][] = [
                'freq_name'     => $freq_name,
                'quick_name'    => $v['quick_app']['name'],
                'title'         => $v['title'],
                'location_key'  => "'{$location_key}'"
            ];

        }
        
        $data = [];
        foreach ($list as $k=>$v)
        {
            foreach ($v['list'] as $vc)
            {
                $freq_name = array_unique(array_column($vc,'freq_name'));
                $title = implode(',',array_column($vc,'title'));
                $location_key = implode(',',array_column($vc,'location_key'));

                $data[] = [
                    'freq_name'     => $freq_name[0],
                    'quick_name'    => $v['quick_name'],
                    'title'         => $title,
                    'location_key'  => $location_key
                ];
            }

        }

        $xlsTools = new XlsTools();
        $xlsTools->start([
            'title' => ['App', '归属频道', '推荐标题', 'location_key'],   //列名
            'type'=>'xls',               //导出的excel的类型
            'name'=>'推荐位详情'                //导出的excel的文件名
        ]);

        foreach ($data as $v)
        {
            $row = [0 => $v['quick_name'], 1 => $v['freq_name'], 2 => $v['title'],
                3 => $v['location_key']
            ];
            $xlsTools->oneData($row);
        }

        /*结束导出*/
        $xlsTools->end();
    }


    /**
     * 批量添加推荐位书籍
     * @return mixed|\think\response\Json
     */
    public function plAddRecommendNovels(){
        if (request()->isAjax()) {
            $params = request()->param();
            $result = $this->validate($params, 'app\backstage\validate\WebsiteValidate.plAddRecommendNovels');
            if (true !== $result) {
                $this->error($result);
            }
            return $this->logic->plAddRecommendNovels($params);
        }else{
            $model = new Recommend();
            $titles = $model->group('title')->column('title');
            $user = request()->_user;
            $agents = getAgents();
            $this->assign('agents', $agents);
            $this->assign('titles', $titles);
            return $this->fetch();
        }
    }

    /**
     * 设置随机
     * @Author: whh
     */
    public function switchRandom()
    {
        $id = input('id');
        $isRandom = input('is_random');
        $params = $this->request->param();
        $result = $this->validate($params, 'app\backstage\validate\WebsiteValidate.switchRandom');
        if (true !== $result) {
            $this->error($result);
        }
        $model = Recommend::get($id);
        if ($model) {
            $model->is_random = $isRandom;
            $model->save();
        } else {
            $this->error('推荐位不存在');
        }
        $this->success();
    }

    /**
     * 任务配置
     * @return mixed
     * @Author: whh
     */
    public function taskConfig()
    {
        $quick_id = input('quick_id');
        $user = request()->_user;
        $qas = \app\common\model\QuickApp::getList();
        /**
         * 任务中心
         */
        $taskData = $this->logic->getTaskData($quick_id);
        $taskData = $this->sortBySortValue($taskData);
        $this->assign('taskData', $taskData);
        $this->assign('qas', $qas);
        $this->assign('quick_id', $quick_id);
        return $this->fetch();
    }

    /**
     * 按sort重新排序
     * @param $array
     * @return mixed
     */
    private function sortBySortValue($array) {
        $sortValues = array_column($array, 'sort');
        array_multisort($sortValues, SORT_ASC, $array);
        return $array;
    }


    /**
     * 开屏管理
     * @return mixed|\think\response\Json
     */
    public function openMange()
    {
        if (request()->isAjax()) {
            $name = input('name');
            $quick_id = input('quick_id');
            $type = input('type');
            $order = input('order');
            $page = input('page', 1);
            $limit = input('limit', 10);
            $orderBy = $order ? $order . ",id desc" : 'id desc';
            $where = [];
            if ($name != '') {
                $where[] = ['name', 'like', "%{$name}%"];
            }
            if ($type != '') {
                $where[] = ['type', '=', $type];
            }
            $ins = '';
            if ($quick_id != '') {
                $ins = "FIND_IN_SET({$quick_id},quick_apps)";
            }
            $query = $list = QuickOpen::where($where);
            if ($ins) {
                $query = $query->whereRaw($ins);
            }
            $count = $query->count();
            $list = $query->order($orderBy)->page($page, $limit)->select();
            return sucJson($list, $count);
        }
        $qas = getMyQas(request()->_user);
        $this->assign('qas', $qas);
        return $this->fetch();
    }


    /**
     * 添加弹窗
     * @return mixed|\think\response\Json
     */
    public function addOpen()
    {
        if (request()->isAjax()) {
            $params = request()->param();
            $result = $this->validate($params, 'app\backstage\validate\OpenValidate.addOpen');
            if (true !== $result) {
                $this->error($result);
            }
            $type = input('type');
            if ($type == 1) {
                if (!isset($params['table_novel_id']) || !$params['table_novel_id']) {
                    $this->error('请设置推广书籍');
                }
                $pic = input('pic');
                if (!$pic) {
                    $this->error('请上传开屏图片');
                }
            } elseif ($type == 2) {
                $activity_id = input('activity_id');
                $activity = RechargeActivity::get($activity_id);
                if (!$activity) {
                    $this->error('请选择推广活动');
                }
                $pic = input('pic');
                if (!$pic) {
                    $params['pic'] = $activity->img_url;
                }
            } elseif ($type == 3) {
                $jump_url = input('jump_url');
                if (!$jump_url) {
                    $this->error('请输入跳转链接');
                }
                $pic = input('pic');
                if (!$pic) {
                    $this->error('请上传开屏图片');
                }
            }
            if ($params['register_type'] == 1 && !$params['register_start'] && !$params['register_end']) {
                $this->error('请设置注册时间范围');
            }
            $logic = new OpenLogic();
            return $logic->setOpen($params);
        }
        $allActivity = RechargeLogic::allActivity();
        $this->assign('allActivity', $allActivity);
        $qas = getMyQas(request()->_user);
        $this->assign('qas', $qas);
        return $this->fetch('website/open/add_open');
    }

    /**
     * 修改开屏
     * @return mixed|\think\response\Json
     */
    public function editOpen()
    {
        $id = input('id');
        $user = request()->_user;
        $role = request()->_role;
        $where[] = ['id', '=', $id];
        $info = QuickOpen::where($where)->find();
        if (!$info) {
            $this->error('弹窗不存在');
        }
        if (request()->isAjax()) {
            $params = request()->param();
            $result = $this->validate($params, 'app\backstage\validate\OpenValidate.editOpen');
            if (true !== $result) {
                $this->error($result);
            }
            $type = input('type');
            if ($type == 1) {
                if (!isset($params['table_novel_id']) || !$params['table_novel_id']) {
                    $this->error('请设置推广书籍');
                }
                $pic = input('pic');
                if (!$pic) {
                    $this->error('请上传开屏图片');
                }
            } elseif ($type == 2) {
                $activity_id = input('activity_id');
                $activity = RechargeActivity::get($activity_id);
                if (!$activity) {
                    $this->error('请选择推广活动');
                }
                $pic = input('pic');
                if (!$pic) {
                    $params['pic'] = $activity->img_url;
                }
            } elseif ($type == 3) {
                $jump_url = input('jump_url');
                if (!$jump_url) {
                    $this->error('请输入跳转链接');
                }
                $pic = input('pic');
                if (!$pic) {
                    $this->error('请上传开屏图片');
                }
            }
            if ($params['register_type'] == 1 && !$params['register_start'] && !$params['register_end']) {
                $this->error('请设置注册时间范围');
            }
            $logic = new OpenLogic();
            return $logic->setOpen($params, $id);
        }
        $qas = getMyQas($user);
        $this->assign('qas', $qas);
        $novel = \app\common\model\Novel::field("id,title,scale,in_shelf,freq_id,class_id")->where('id', 'in', $info->novel_id)->with(['freq', 'novel_class'])->findOrFail();
        $this->assign('novel', $novel);
        $this->assign('info', $info);
        $allActivity = RechargeLogic::allActivity();
        $this->assign('allActivity', $allActivity);
        $this->assign('role', $role);
        return $this->fetch('website/open/edit_open');
    }


    /**
     * 设置开屏权重
     */
    public function setOpenWgt()
    {
        $id = input('id');
        $wgt = input('wgt');
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\OpenValidate.setOpenWgt');
        if (true !== $result) {
            $this->error($result);
        }
        $where[] = ['id', '=', $id];
        QuickOpen::where($where)->setField('wgt', $wgt);
        $this->success();
    }


    /**
     * 设置状态
     */
    public function setOpenStatus()
    {
        $params = $this->request->param();
        $result = $this->validate($params, 'app\backstage\validate\OpenValidate.setOpenStatus');
        if (true !== $result) {
            $this->error($result);
        }
        $id = input('id');
        $status = input('status');
        $where[] = ['id', '=', $id];
        $pop = QuickOpen::where($where)->find();
        if (!$pop) {
            $this->error('弹窗不存在');
        }
        $pop->status = $status;
        $pop->save();
        $this->success();
    }

    /**
     * 删除开屏
     * @throws \Exception
     */
    public function delOpen()
    {
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\OpenValidate.delOpen');
        if (true !== $result) {
            $this->error($result);
        }
        $id = input('id');
        $where[] = ['id', '=', $id];
        QuickOpen::where($where)->delete();
        $this->success();
    }

    /**
     * 设置任务上下架状态
     * @Author: whh
     */
    public function setTaskDisplay()
    {
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\WebsiteValidate.setTaskDisplay');
        if (true !== $result) {
            $this->error($result);
        }
        $quick_id = input('quick_id');
        $user = request()->_user;
        $role = request()->_role;
        if($role->alias == 'agent')
        {
            $myQIds = Db::table('quick_app_mp_manager')->where('uid',$user->id)->column('quick_id');
            if(empty($myQIds))
            {
                return errJson('无权限操作此App');
            }
            if (!in_array($quick_id, $myQIds)) {
                return errJson('无权限操作此App');
            }
        }

        $this->logic->setTaskDisplay();
        $this->success();
    }
}